package com.yager.service.impl;

import com.yager.mapper.AccuseMapper;
import com.yager.model.Accuse;
import com.yager.service.AccuseService;
import com.yager.util.NameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class AccuseServiceImpl implements AccuseService {

    @Autowired
    private AccuseMapper accuseMapper;

    @Override
    public void addNewAccuse(MultipartFile[] file, String accusetype, String accusedescription,String userid,String beAccusedId) {
        //生成新的id
        String max=accuseMapper.findMaxId();
        max=max.substring(1);

        Integer i=Integer.parseInt(max);
        i++;
        String id="A";

        if(i<10) id=id+"00"+i.toString();
        else if(i<100) id=id+"0"+i.toString();
        else id=id+i.toString();
        //生成id结束 新的id变量名为id

        //日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String datetime=sdf.format(new Date());
        Date date= null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //日期变量名为date

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String dataPath = "house" + "/" + simpleDateFormat.format(new Date()) + "/" + id;
        System.out.println(dataPath);

        List<File> fileList = new ArrayList<File>();
        for(int j = 0; j < file.length; j++){
            String oldImgName = file[j].getOriginalFilename();
            String imgSuffix = oldImgName.substring(oldImgName.lastIndexOf("."));
            NameUtil nameUtil = new NameUtil();
            String imgName = nameUtil.GeneratorhouseImgName() + imgSuffix;
            File targetfile = new File("C://yagerImg/" + dataPath);
            if(!targetfile.exists()) targetfile.mkdirs();
            /*指定图片名称*/
            File trueTargetFile = new File(targetfile, imgName);
            try {
                file[j].transferTo(trueTargetFile);
            }catch (IOException e){
                e.printStackTrace();
            }
        }

        Accuse accuse = new Accuse(id,userid,accusetype,accusedescription,dataPath,true,date);
        accuse.setBeaccusedid(beAccusedId);

        accuseMapper.insert(accuse);
    }

    @Override
    public void endAccuse(Accuse accuse) {
        accuse.setAccusestatus(false);
        accuseMapper.updateByPrimaryKeySelective(accuse);
    }

    @Override
    public List<Accuse> getAllAccuse() {
        List<Accuse> list=accuseMapper.selectAllAccues();
        return list;
    }

    @Override
    public Accuse getOneAccuseById(String accuseid) {
        Accuse accuse=accuseMapper.selectByPrimaryKey(accuseid);
        return accuse;
    }
}
